<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app'
import { computed, ref } from 'vue'
import type { SwiperItem } from '@/types/home'
import { getSwiperDataAPI } from '@/services/home'
import { getCategoryDataAPI } from '@/services/category'
import type { CategoryOneItem, CategoryTwoItem } from '@/types/category'
import Skeleton from './components/Skeleton.vue'
// @ts-ignore
import { debounce } from 'lodash'

// 轮播图列表
const swiperList = ref<SwiperItem[]>([])
// 分类数据
const categoryList = ref<CategoryOneItem[]>([])
// 当前激活的一级分类的下标
const activeCategoryIndex = ref(0)
// 数据是否加载完毕
let isFinish = ref(false)
// scroll-view滚动的top值
let scrollTop = ref(0)

// 获取当前二级分类的数据
/* computed() 会自动从其计算函数的返回值上推导出类型,也可以通过泛型参数显式指定类型
computed<type>(()=>xxxx)计算属性的TS写法为其标注类型 */
const subCategoryList = computed<CategoryTwoItem[]>(
  // categoryList可能为[] 使用可选链 ? 访问其属性 当其结果为undefined时返回空数组
  () => categoryList.value[activeCategoryIndex.value]?.children || [],
)

// 获取轮播图数据
const getSwiperData = async () => {
  const result = await getSwiperDataAPI(2)
  // 存储轮播图数据
  swiperList.value = result.result
}
// 获取商品分类数据
const getCategoryData = async () => {
  const result = await getCategoryDataAPI()
  // 存储商品分类数据
  categoryList.value = result.result
}
// 当二级分类的scroll-view滚动时触发
const onScroll = debounce((event: UniHelper.ScrollViewOnScrollEvent) => {
  // 优化:防抖 当滚动行为结束0.5后再更新scrollTop的值,防止频繁修改scrollTop的值导致页面抖动
  // 动态修改scrollTop的值
  scrollTop.value = event.detail.scrollTop
}, 500)
// 切换一级分类的回调
const onchangeCategory = (index: number) => {
  // 修改当前激活的一级分类的下标
  activeCategoryIndex.value = index
  // scroll-view滚动到顶部
  scrollTop.value = 0
}

// 页面加载
onLoad(async () => {
  // 显示骨架屏
  isFinish.value = false
  // 使用Promise.all() 并行执行多个异步操作
  await Promise.all([getSwiperData(), getCategoryData()])
  // 数据全部获取完毕 隐藏骨架屏
  isFinish.value = true
})
</script>

<template>
  <Skeleton v-if="!isFinish" />
  <view v-else class="viewport">
    <!-- 搜索框 -->
    <view class="search">
      <view class="input">
        <text class="icon-search">女靴</text>
      </view>
    </view>
    <!-- 分类 -->
    <view class="categories">
      <!-- 左侧：一级分类 -->
      <scroll-view class="primary" scroll-y>
        <view
          v-for="(categoryOne, index) in categoryList"
          :key="categoryOne.id"
          class="item"
          :class="{ active: index === activeCategoryIndex }"
          @tap="onchangeCategory(index)"
        >
          <text class="name"> {{ categoryOne.name }} </text>
        </view>
      </scroll-view>
      <!-- 右侧：二级分类 -->
      <scroll-view
        class="secondary"
        scroll-y
        :scroll-top="scrollTop"
        @scroll="onScroll"
      >
        <!-- 轮播图 -->
        <CustomSwiper class="banner" :swiperData="swiperList" />
        <!-- 内容区域 -->
        <view class="panel" v-for="item in subCategoryList" :key="item.id">
          <view class="title">
            <text class="name">{{ item.name }}</text>
            <navigator class="more" hover-class="none">全部</navigator>
          </view>
          <view class="section">
            <navigator
              v-for="goods in item.goods"
              :key="goods.id"
              class="goods"
              hover-class="none"
              :url="`/pages/goods/goods?id=${goods.id}`"
            >
              <image class="image" :src="goods.picture"></image>
              <view class="name ellipsis">{{ goods.name }}</view>
              <view class="price">
                <text class="symbol">¥</text>
                <text class="number">{{ goods.price }}</text>
              </view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}
.viewport {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.search {
  padding: 0 30rpx 20rpx;
  background-color: #fff;
  .input {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 64rpx;
    padding-left: 26rpx;
    color: #8b8b8b;
    font-size: 28rpx;
    border-radius: 32rpx;
    background-color: #f3f4f4;
  }
}
.icon-search {
  &::before {
    margin-right: 10rpx;
  }
}
/* 分类 */
.categories {
  flex: 1;
  min-height: 400rpx;
  display: flex;
}
/* 一级分类 */
.primary {
  overflow: hidden;
  width: 180rpx;
  flex: none;
  background-color: #f6f6f6;
  .item {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 96rpx;
    font-size: 26rpx;
    color: #595c63;
    position: relative;
    &::after {
      content: '';
      position: absolute;
      left: 42rpx;
      bottom: 0;
      width: 96rpx;
      border-top: 1rpx solid #e3e4e7;
    }
  }
  .active {
    background-color: #fff;
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      width: 8rpx;
      height: 100%;
      background-color: #27ba9b;
    }
  }
}
.primary .item:last-child::after,
.primary .active::after {
  display: none;
}
/* 二级分类 */
.secondary {
  background-color: #fff;
  .banner {
    height: 220rpx;
    margin-bottom: 30rpx;
    .carousel {
      height: 220rpx;
      margin: 0 30rpx 20rpx;
      border-radius: 4rpx;
      overflow: hidden;
      swiper {
        border-radius: 15rpx;
        overflow: hidden;
        background-color: #fff;
        .image {
          border-radius: 15rpx;
        }
      }
    }
  }
  .panel {
    margin: 0 30rpx 0rpx;
  }
  .title {
    height: 60rpx;
    line-height: 60rpx;
    color: #333;
    font-size: 28rpx;
    border-bottom: 1rpx solid #f7f7f8;
    .more {
      float: right;
      padding-left: 20rpx;
      font-size: 24rpx;
      color: #999;
    }
  }
  .more {
    &::after {
      font-family: 'erabbit' !important;
      content: '\e6c2';
    }
  }
  .section {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    padding: 20rpx 0;
    .goods {
      width: 150rpx;
      margin: 0rpx 30rpx 20rpx 0;
      &:nth-child(3n) {
        margin-right: 0;
      }
      image {
        width: 150rpx;
        height: 150rpx;
      }
      .name {
        padding: 5rpx;
        font-size: 22rpx;
        color: #333;
      }
      .price {
        padding: 5rpx;
        font-size: 18rpx;
        color: #cf4444;
      }
      .number {
        font-size: 24rpx;
        margin-left: 2rpx;
      }
    }
  }
}
</style>
